<?php
declare (strict_types = 1);
namespace app\topic\model\admin;

use app\common\logic\Attach;
use app\common\model\BaseModel;
use app\school\model\school\School;
use think\model\concern\SoftDelete;

/**
 * 定义资讯数据模型
 */
class Topic extends BaseModel
{
    use SoftDelete;
    /**
     * 表名
     * @var string
     */
    protected $name = 'topic';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    /**
     * 发布时间
     * @var string
     */
    protected $createTime = 'publish_time';

    /**
     * 时间显示格式
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';
    /**
     * 全局查询范围
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'               => 'integer',
        'fullcategorypath' => 'json',
    ];

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-14
     * @return   string
     */
    public static function onBeforeInsert($course)
    {
        // 机构
        $course->mhm_id = $course->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 封面图地址
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-01
     * @return   [type]                         [description]
     */
    public function coverUrl()
    {
        return $this->belongsTo(Attach::class, 'cover')->bind(['cover_url' => 'fileurl']);
    }

    /**
     * 机构
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-02
     * @return   [type]                         [description]
     */
    public function school()
    {
        return $this->belongsTo(School::class, 'mhm_id')->bind(['school_name' => 'title']);
    }

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 机构条件
        $query->where('mhm_id', request()->mhm_id);
    }

}
